package com.sike.netty.rumen.shizhan_rengong.common;

import io.netty.channel.Channel;
import lombok.SneakyThrows;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class ChannelSessionMemory {

    private final static Map<String, Channel> clientChannelMap = new ConcurrentHashMap<>();

    /**
     * 人工坐席列表
     */
    private final static BlockingQueue<String> rgQueue = new ArrayBlockingQueue<>(1024);

    /**
     * 在等待的用户
     */
    private final static BlockingQueue<String> waitingUserQueue = new ArrayBlockingQueue<>(1024);

    /**
     * 用户-人工的映射关系
     */
    private final static Map<String,String> userAndRgMap = new ConcurrentHashMap<>();

    /**
     * 人工 - 用户的映射关系
     */
    private final static Map<String,String> rgAndUserMap = new ConcurrentHashMap<>();

    /**
     * 保存客户端 Channel
     * @param key
     * @param channel
     */
    public static void putClientChannel(String key,Channel channel) {
       clientChannelMap.put(key,channel);
    }

    /**
     * 获取客户端 Channel
     * @param key
     * @return
     */
    public static Channel getClientChannel(String key) {
        return clientChannelMap.get(key);
    }

    /**
     * 添加人工坐席
     * @param channelId
     */
    @SneakyThrows
    public static void putRg(String channelId) {
        rgQueue.put(channelId);
    }

    /**
     * 获取人工坐席
     * @return
     */
    @SneakyThrows
    public static String getRgChannelId() {
        return rgQueue.poll(30,TimeUnit.SECONDS);
    }


    /**
     * 加入等待队列
     * @param channelId
     */
    @SneakyThrows
    public static void putWaitingUser(String channelId) {
        waitingUserQueue.offer(channelId,30,TimeUnit.SECONDS);
    }

    /**
     * 删除用户等待队列
     * @param channelId
     */
    public static void deleteWaitingUser(String channelId) {
        waitingUserQueue.remove(channelId);
    }


    /**
     * 获取等待用户
     * @return
     */
    @SneakyThrows
    public static String getWaitingUser() {
        return waitingUserQueue.poll(5,TimeUnit.SECONDS);
    }

}

